/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Animal;
import Model.AnimalType;
import Model.Being;
import Model.Board;
import Model.Building;
import Model.Clan;
import Model.Game;
import Model.Location;
import Model.Parameter;
import Model.Square;
import Model.Statistics;
import Model.Villager;
import View.BoardMap;
import View.GameView;
import View.MainView;
import View.ParamPanel;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author elach_000
 */
public class InitController {

    private MainView mainView;
    private Game game;
    private GameView gameView;

    public InitController() {
        this.game = new Game();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                InitController init = new InitController();
                init.mainView = new MainView(init);
                init.mainView.setVisible(true);
            }
        });
    }

    public void initStatistics() {
        ParamPanel paramPanel = mainView.getParamPanel();

        //Animals Creation
        int nbLions = Integer.parseInt(paramPanel.getLionsTextField().getText());
        int nbDoes = Integer.parseInt(paramPanel.getDoesTextField().getText());
        int nbCows = Integer.parseInt(paramPanel.getElephantsTextField().getText());

        int nbWood = 0;
        int nbGold = 0;
        int nbStone = 0;
        int nbWater = 0;

        Square[][] gb = this.game.getBoard().getGameBoard();
        for (int i = 0; i < this.game.getBoard().getWidth(); i++) {
            for (int j = 0; j < this.game.getBoard().getHeight(); j++) {

                switch (gb[i][j].getType()) {
                    case Water:
                        nbWater++;
                        break;
                    case Wood:
                        nbWood++;
                        break;
                    case Gold:
                        nbGold++;
                        break;
                    case Stone:
                        nbStone++;
                        break;
                }
            }
        }

        Statistics.setNbDoes(nbDoes);
        Statistics.setNbLions(nbLions);
        Statistics.setNbCows(nbCows);

        Statistics.setNbMapWood(nbWood * 50);
        Statistics.setNbMapWater(nbWater * 50);
        Statistics.setNbMapStone(nbStone * 50);
        Statistics.setNbMapGold(nbGold * 50);
    }

    public void startGame(String mode) {
        //Init Game
        if (mode.equals("normal")) {
            initParameters();

            Board b = initMap();
            this.game.setBoard(b);
            initGame(b);
            this.game.refreshBoard(false);
            if (this.game.getNumberTotalOfBeings() > 0) {
                this.adaptGameSpeed();
            }
            this.initStatistics();

            //Init GameView
            BoardMap boardMap = modelMaptoViewMap();
            this.gameView = new GameView(boardMap);
            this.gameView.setVisible(true);

            //Link view to model
            this.game.getBoard().addObserver(this.gameView.getBoard());
            this.game.getClanList().get(0).addObserver(this.gameView.getMonitor());
            this.gameView.getMonitor().setGame(this.game);
            this.game.addObserver(this.gameView.getMonitor());

            //this.game.initTestMode();
            //Starting the game
            Thread t = new Thread(new Runnable() {
                public void run() {
                    game.startDebugMode();
                }
            });

            t.start();

        } else if (mode.equals("debug")) {
            this.game = new Game();

            this.game.initDebugMode();
            this.game.startDebugMode();
        } else { //GUI Demo mode
            this.gameView = new GameView();
            this.gameView.setVisible(true);
        }

    }

    private void initParameters() {
        ParamPanel paramPanel = mainView.getParamPanel();

        Parameter.setHeightBoard(Integer.parseInt(paramPanel.getMapHeightTextBox().getText()));
        Parameter.setWidthBoard(Integer.parseInt(paramPanel.getMapWidthTextBox().getText()));

        if (paramPanel.getGameHoldCheckBox().isSelected()) {
            Parameter.setGameHold(true);
        }

        if (paramPanel.getBeingHoldCheckBox().isSelected()) {
            Parameter.setBeingHold(true);
        }

        //setting default speed values
        if (paramPanel.getInstantRadioButton().isSelected()) {
            Parameter.setGameDelay(0);
        } else if (paramPanel.getFastRadioButton().isSelected()) {
            Parameter.setGameDelay(50);
        } else if (paramPanel.getMediumRadioButton().isSelected()) {
            Parameter.setGameDelay(100);
        } else if (paramPanel.getSlowRadioButton().isSelected()) {
            Parameter.setGameDelay(200);
        }

    }

    private void adaptGameSpeed() {
        ParamPanel paramPanel = mainView.getParamPanel();
        if (paramPanel.getInstantRadioButton().isSelected()) {
            Parameter.setGameDelay(0);
        } else if (paramPanel.getFastRadioButton().isSelected()) {
            Parameter.setGameDelay(50 * 10 / this.game.getNumberTotalOfBeings());
        } else if (paramPanel.getMediumRadioButton().isSelected()) {
            Parameter.setGameDelay(100 * 10 / this.game.getNumberTotalOfBeings());
        } else if (paramPanel.getSlowRadioButton().isSelected()) {
            Parameter.setGameDelay(200 * 10 / this.game.getNumberTotalOfBeings());
        }

    }

    private void initGame(Board board) {
        this.game = new Game(board);
        this.game.setClanList(new ArrayList<Clan>());
        this.game.setAnimalList(new HashMap<Integer, Animal>());

        ParamPanel paramPanel = mainView.getParamPanel();

        //Clan Creation
        String clanName = paramPanel.getClanNameTextField().getText();

        int qgX = Integer.parseInt(paramPanel.getQgXTextField().getText());
        int qgY = Integer.parseInt(paramPanel.getQgYTextField().getText());

        int nbHunters = Integer.parseInt(paramPanel.getHuntersTextField().getText());
        int nbBuilders = Integer.parseInt(paramPanel.getBuildersTextField().getText());
        int nbGoldminer = Integer.parseInt(paramPanel.getGoldminerTextField().getText());
        int nbStoneminer = Integer.parseInt(paramPanel.getStoneminerTextField().getText());
        int nbLumberjack = Integer.parseInt(paramPanel.getLumberjackTextField().getText());
        int nbFisherman = Integer.parseInt(paramPanel.getFishermanTextField().getText());

        this.game.createClan(clanName, qgX, qgY, nbHunters, nbBuilders, nbStoneminer, nbLumberjack, nbGoldminer, nbFisherman);
        this.game.createBuilding(new Location(qgX, qgY));
        //Animals Creation

        int nbLions = Integer.parseInt(paramPanel.getLionsTextField().getText());
        int nbDoes = Integer.parseInt(paramPanel.getDoesTextField().getText());
        int nbElephants = Integer.parseInt(paramPanel.getElephantsTextField().getText());

        game.createAnimal(AnimalType.Doe, nbDoes);
        game.createAnimal(AnimalType.Lion, nbLions);
        game.createAnimal(AnimalType.Elephant, nbElephants);
    }

    private Board initMap() {
        Board b = new Board(Parameter.getWidthBoard(), Parameter.getHeightBoard());

        ParamPanel paramPanel = mainView.getParamPanel();

        double[] bounds;

        if (paramPanel.getAquaticMapRadioButton().isSelected()) {
            bounds = Parameter.getResourcesParametersForAquaticMap();
        } else if (paramPanel.getPlainMapRadioButton().isSelected()) {
            bounds = Parameter.getResourcesParametersForPlainMap();
        } else {
            bounds = Parameter.getResourcesParametersForNormalMap();
        }

        b.initGameBoard(10, 5, 0.2, bounds);

        return b;
    }

    public BoardMap modelMaptoViewMap() {
        BoardMap boardMap = new BoardMap();
        for (int i = 0; i < game.getBoard().getWidth(); i++) {
            for (int j = 0; j < game.getBoard().getHeight(); j++) {

                //BASE TILE MAPPING
                int baseTileID = -1;
                //TO DO : Map with tileset
                switch (game.getBoard().getGameBoard()[i][j].getType()) {
                    case Wood:
                        baseTileID = 1;
                        boardMap.getMap()[i][j].addHeightTile(122);
                        break;
                    case Plain:
                        baseTileID = 1;
                        break;
                    case Gold:
                        baseTileID = 10;
                        break;
                    case Stone:
                        baseTileID = 1;
                        boardMap.getMap()[i][j].addHeightTile(50);
                        break;
                    case Mountain:
                        baseTileID = 54;
                        boardMap.getMap()[i][j].addHeightTile(54);
                        boardMap.getMap()[i][j].addHeightTile(54);
                        boardMap.getMap()[i][j].addHeightTile(54);
                        break;
                    case Water:
                        baseTileID = 85;
                        break;
                }

                boardMap.getMap()[i][j].setBaseTile(baseTileID);


                //CHARACTER MAPPING
                int characterTileID = -1;
                Being being = game.getBoard().getGameBoard()[i][j].getBeingReference();
                if (being != null) {
                    //TO DO: Switch case Being (Lion, hunter, etc...)
                    if (being.isAnimal()) {
                        switch (((Animal) being).getType()) {
                            case Doe:
                                characterTileID = 11;
                                break;
                            case Lion:
                                characterTileID = 14;
                                break;
                            case Elephant:
                                characterTileID = 12;
                                break;
                        }
                    } else {
                        switch (((Villager) being).getActivity()) {
                            case Fisherman:
                                characterTileID = 5;
                                break;
                            case Builder:
                                characterTileID = 10;
                                break;
                            case Stoneminer:
                                characterTileID = 9;
                                break;
                            case Goldminer:
                                characterTileID = 9;
                                break;
                            case Hunter:
                                characterTileID = 6;
                                break;
                            case Lumberjack:
                                characterTileID = 7;
                                break;
                        }
                    }
                }

                boardMap.getMap()[i][j].setCharacter(characterTileID);
                //BUILDING MAPPING
                Building building = game.getBoard().getGameBoard()[i][j].getBuildingReference();

                if (building != null) {
                    boardMap.getMap()[i][j].setBuilding(0);
                }
            }
        }

        return boardMap;

    }
}
